package com.bnz.servlet;

import com.alibaba.fastjson.JSON;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentVo;
import com.bnz.service.StudentService;
import com.bnz.service.impl.StudentServiceImpl;
import com.bnz.util.R;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    private StudentService studentService;
    @Override
    public void init() throws ServletException {
        studentService = new StudentServiceImpl();
    }
    // 根据请求参数调用不同的处理方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf8");
        // 1.1 得到请求参数
        String cmd = req.getParameter("cmd");
        // 1.2 判断请求参数是否有值
        if (StringUtils.isNotBlank(cmd)) {
            if ("list".equals(cmd)) {
                list(req, resp);
            }
            if ("findByPage".equals(cmd)) {
                findByPage(req, resp);
            }
            if ("search".equals(cmd)) {
                search(req, resp);
            }
            if ("save".equals(cmd)) {
                save(req, resp);
            }
            if ("update".equals(cmd)) {
                update(req, resp);
            }
            if ("delete".equals(cmd)) {
                delete(req, resp);
            }
        }
    }



    // 2. 列表学生
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.查询所有学生
        List<Student> students = studentService.findAll();
        // 2. 指定输出内容
        resp.getWriter().println(JSON.toJSONString(students));
        resp.getWriter().flush();
    }

    // 3.分页查询
    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 3.1 得到分页参数
        int page = Integer.parseInt(req.getParameter("page"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        //查询得到分页数据
        PageResult<Student> pr = studentService.findByPage(page, pageSize);

        resp.getWriter().println(JSON.toJSONString(pr));
        resp.getWriter().flush();
    }

    // 4.条件查询带分页
    private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("GBK");
        //4.1 得到分页参数
        int page = Integer.parseInt(req.getParameter("page"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        String data = req.getParameter("data");
        System.out.println("data = " + data);
        StudentVo studentVo = JSON.parseObject(data, StudentVo.class);
        //4.2 开始查询
        PageResult<Student> pr = studentService.search(page,pageSize,studentVo);
        //4.3 指定输出内容
        resp.getWriter().println(JSON.toJSONString(pr));
        resp.getWriter().flush();
    }


    // 5.保存学生
    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 5.1 得到传来的student对象
        String data = req.getParameter("data");
        // 5.2 专成学生对象
        Student student  = JSON.parseObject(data,Student.class);

        System.out.println("s = " + student);

        try {
            studentService.add(student);

            resp.getWriter().println(JSON.toJSONString(R.ok()));
            resp.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
            resp.getWriter().println(JSON.toJSONString(R.error()));
            resp.getWriter().flush();
        }

    }

    // 6. 修改学生
    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String data = req.getParameter("data");
        Student student = JSON.parseObject(data, Student.class);
        // 调用函数修改
        studentService.updateById(student);

        try {
            studentService.updateById(student);
            resp.getWriter().println(JSON.toJSONString(R.ok()));
            resp.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
            resp.getWriter().println(JSON.toJSONString(R.error()));
            resp.getWriter().flush();
        }
    }


    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String ids = req.getParameter("ids");
        String[] split = ids.split(",");
        List<String> sids = Arrays.asList(split);

        try {
            studentService.delete(sids);
            resp.getWriter().println(JSON.toJSONString(R.ok("删除学生成功")));
            resp.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
            resp.getWriter().println(JSON.toJSONString(R.error()));
            resp.getWriter().flush();
        }
    }

    @Override
    public void destroy() {
        super.destroy();
    }
}
